#include "BmpBtn.h"
#include "Gui.h"
#include "Container.h"
#include "UtilInclude.h"
#include "../debug_new.h"

int g_ID = 0;

void CALLBACK LongPessedPress(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
	if ( g_ID!=0 )
	{
		PostMessage(hwnd,WM_LONG_BTN_PRESS, g_ID,1);
	}
}

void CALLBACK LongPessedStart(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
	KillTimer(hwnd,0x2013);
	SetTimer(hwnd,0x2014,350,LongPessedPress);
}

CBmpBtn::CBmpBtn(void)
{
	Init();
}

CBmpBtn::CBmpBtn(UINT nID):CControl(nID)
{
	Init();
}

CBmpBtn::CBmpBtn(wstring strCaption,UINT nID)
:	CControl(strCaption,nID)
{
	Init();
}

CBmpBtn::CBmpBtn(DWORD dwStyle, wstring strCaption,const CRect &rc, UINT nID)
:	CControl(dwStyle,strCaption, rc, nID)
{
	Init();
}

CBmpBtn::CBmpBtn(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID)
:	CControl(dwStyle, strCaption, x, y, cx, cy, nID)
{
	Init();
}

CBmpBtn::~CBmpBtn()
{
	if(m_pImgLstBtn)
	{
		delete m_pImgLstBtn;
	}
}

void CBmpBtn::Init()
{
	m_pImgLstBtn = NULL;
	m_nIconNum = 4;
	m_szTextOffset.SetSize( 0,6 ); 
	m_dwAlign = DT_SINGLELINE|DT_TOP|DT_CENTER;
	m_bRestoreBkgnd = FALSE;
	m_szIcon.SetSize( 0,0 );
}

wstring GetRectShowStr(wstring str,CRect rt,HFONT hFont)
{
	wstring strTmp = str;		
	CRect rect(0, 0, 0, 0);
	//	CPaintDC dc(m_pContainer->m_hWnd);	
	CDC dc;
	dc.CreateCompatibleDC(NULL);
	HFONT oldFont = (HFONT)::SelectObject(dc.m_hDC, hFont);
	dc.DrawText( strTmp.c_str(), strTmp.length(), &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER );
	//DrawText(dc.m_hDC, strTmp.c_str(), strTmp.length(), &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER);
	if (rect.Width() <= rt.Width() )
	{
		return strTmp; 
	}
	::DrawText(dc.m_hDC,L"...", 3, &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER);
	int width = rect.Width();

	int Mid, Low, Hei;
	Low = 0;
	Hei = strTmp.length();
	Mid = ( Hei + Low )/2;
	DrawText(dc.m_hDC, strTmp.c_str(), strTmp.length(), &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER);
	for ( ;( (   abs(rect.Width()- rt.Width() + width ) > 2 && rect.Width()- rt.Width() + width < 0 ) || rect.Width()- rt.Width()- width > 0 )  && Low <= Hei; )
	{
		if( rect.Width() + width > rt.Width() )
		{
			Hei = Mid -1;
		} else {
			Low = Mid +1;
		}
		Mid = ( Low + Hei )/2;	
		DrawText(dc.m_hDC, strTmp.c_str(), Mid, &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER);	
	}
	for (  ;(  rt.Width()- rect.Width()-  2 ) < width && Mid > 0 ; )
	{
		Mid --;
		DrawText(dc.m_hDC, strTmp.c_str(), Mid, &rect, DT_SINGLELINE|DT_CALCRECT |DT_VCENTER);	

	}	
	TCHAR tcTemp[64];
	TCHAR tcTitle[64];
	memset(tcTemp,0,64*sizeof(TCHAR));
	memset(tcTitle,0,64*sizeof(TCHAR));
	memcpy(tcTemp,strTmp.c_str(),sizeof(TCHAR)*Mid);
	swprintf(tcTitle,L"%s...",tcTemp);
	strTmp = tcTitle;
	::SelectObject(dc.m_hDC, oldFont);
	return strTmp;
}

void CBmpBtn::Paint(CDC* pDC)
{
	if(m_rcControl.IsRectEmpty())
	{
		return ;
	}

	if(!IsVisible())
	{
		return;
	}

	if(m_bRestoreBkgnd)
	{
		PaintBackGnd(pDC);
	}

	int nIndex = 0;
	COLORREF clrOld = RGB(255,255,255);
	if(4 == m_nIconNum)
	{
		if(STATE_NORMAL & m_nState)
		{
			nIndex = 0;
			clrOld = pDC->SetTextColor(m_clrTxtNormal);
		}
		else if(STATE_FOCUS & m_nState)
		{
			nIndex = 1;
			clrOld = pDC->SetTextColor(m_clrTxtActive);
		}
		else if(STATE_PUSHED & m_nState)
		{
			nIndex = 2;
			clrOld = pDC->SetTextColor(m_clrTxtActive);
		}
		else
		{
			nIndex = 3;
			clrOld = pDC->SetTextColor(m_clrTxtNormal);
		}
	}
	else if(2 == m_nIconNum)
	{
		if(STATE_NORMAL & m_nState)
		{
			nIndex = 0;
			clrOld = pDC->SetTextColor(m_clrTxtNormal);
		}
		else
		{
			nIndex = 1;
			clrOld = pDC->SetTextColor(m_clrTxtActive);
		}
	}
	else
	{
		nIndex = 0;
		clrOld = pDC->SetTextColor(m_clrTxtNormal);
	}

	if(m_pImgLstBtn)
	{
		CPoint pt;
		pt.x = m_rcControl.left + (m_rcControl.Width() - m_szIcon.cx) / 2;
		int nTmp = (m_rcControl.Height() - m_szIcon.cy - m_szTextOffset.cy) / 2;
		if(nTmp < 0)
		{
			nTmp = 0; //- nTmp;
		}
		pt.y = m_rcControl.top + nTmp;

		m_pImgLstBtn->Draw(pDC,nIndex,pt/*m_rcControl.TopLeft()*/);
	}

	if(m_strCaption.length())
	{
		BEGIN_FONT_SELECTION

		pDC->SetBkMode(TRANSPARENT);

		CRect rcTmp = m_rcControl;
		rcTmp.top += m_szTextOffset.cy;
		if(m_dwAlign&DT_RIGHT)
		{
			rcTmp.right -= m_szTextOffset.cx;
		}
		else
		{
			rcTmp.left += m_szTextOffset.cx;
		}

		if(m_dwAlign&16)
		{
			pDC->DrawText(m_strCaption.c_str(),m_strCaption.size(),rcTmp,m_dwAlign);
		}
		else
		{
			wstring strTemp = m_strCaption;
			strTemp = GetRectShowStr(m_strCaption,rcTmp,m_pFont->m_hFont);
			pDC->DrawText(strTemp.c_str(),strTemp.size(),rcTmp,m_dwAlign);
		}

		
		pDC->SetTextColor(clrOld);

		END_FONT_SELECTION
	}
}

void CBmpBtn::SetBmp(HBITMAP pBmpBtn,BOOL bTransparent,COLORREF clrTrans)
{
	if(NULL == pBmpBtn)
	{
		return;
	}

	CSize sz;
	BITMAP bm = { 0 };
	::GetObject(pBmpBtn, sizeof(BITMAP), &bm);
	sz.cx = bm.bmWidth;
	sz.cy = bm.bmHeight;

	m_szIcon.cx = sz.cx / m_nIconNum;
	m_szIcon.cy = sz.cy;

	if(m_pImgLstBtn)
	{
		delete m_pImgLstBtn;
		m_pImgLstBtn = NULL;
	}

	m_pImgLstBtn = new CImageList();
	m_pImgLstBtn->Create(m_szIcon.cx,m_szIcon.cy,bTransparent);
	m_pImgLstBtn->Add(pBmpBtn,clrTrans);
}

 void CBmpBtn::Click(DWORD dwClick, POINT pt)
 {
	 if(!IsEnabled())
	 {
		 return;
	 }

	 if(!m_bVisibale)
	 {
		 return;
	 }

	 if(!this->GetContainer() || !this->GetContainer()->m_hWnd)
	 {
		 return;
	 }

	 if(m_rcControl.PtInRect(pt))
	 {
		 if(dwClick & CLICK_DOWN)
		 {
			 m_nState = STATE_PUSHED;
			 SetTimer(m_pContainer->m_hWnd,0x2013,1500,LongPessedStart);
			 g_ID=m_nID;
		 }
		 else if(dwClick & CLICK_UP)
		 {
			 if(m_pContainer->m_hWnd)
			 {
				 KillTimer(m_pContainer->m_hWnd,0x2013);
				 KillTimer(m_pContainer->m_hWnd,0x2014);
				 PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
				 CUtil::PlayBeepSound();

			 }
			 m_nState = STATE_NORMAL;
		 }
	 }
	 else
	 {
		 m_nState = STATE_NORMAL;
	 }

	 if(m_pContainer && m_pContainer->m_hWnd)
	 {
		 CClientDC dc(m_pContainer->m_hWnd);
		 Paint(&dc);
	 }

 }
